package org.example.controller;

import org.example.model.table.Course;
import org.example.model.vo.CourseVo;
import org.example.service.CourseService;
import org.example.util.CommonRequest;
import org.example.util.CommonRequestHandle;
import org.example.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/course")
@CrossOrigin(origins = "*",maxAge = 3600)
public class CourseController {
    @Resource
    CourseService courseService;
    @PostMapping("/addCourse")
    public Result addCourse(@RequestBody CommonRequest commonRequest){
        Course request = CommonRequestHandle.getRequest(commonRequest, Course.class);
        return courseService.addCourse(request);
    }
    @DeleteMapping("/deleteCourse")
    public Result deleteCourseById(@RequestParam(required = false) Long id){
        if(id == null)return Result.error().data("msg","请给一个课程id").setCode(403);
        return courseService.deleteCourseById(id);
    }
    @PostMapping("/changeCourse")
    public Result changeCourse(@RequestBody CommonRequest commonRequest){
        Course request = CommonRequestHandle.getRequest(commonRequest, Course.class);
        return courseService.changeCourse(request);
    }
    @GetMapping("/getCourse")
    public Result getCourseById(@RequestParam(required = false) Long id){
        if(id == null)return Result.error().data("msg","请给一个课程id").setCode(403);
        return courseService.getCourseById(id);
    }

    @GetMapping("/getCoursePage")
    public Result getCoursePage(
            @RequestParam(defaultValue = "1",required = false) Integer page,
            @RequestParam(defaultValue ="10",required = false, name = "limit") Integer size,
            CourseVo courseVo
    ){
        return courseService.getCoursePage(page, size, courseVo);
    }

    @GetMapping("/getAllCourse")
    public Result getAllPageCoursePage(){
        return courseService.getAllCourse();
    }
}
